<?php

namespace brand\components;

use brand\logic\BrandHelper;
use brand\logic\Database;

class Checkbox extends Component
{
    public function _init()
    {
        $this->component = 'checkbox';
    }





    /**
     * 表单值字段过滤
     */
    public function checkFormValue()
    {
        if ($this->value && !is_array($this->value) && is_array(json_decode($this->value,1))) $this->value = json_decode($this->value,1);
        if ($this->value && !is_array($this->value))  $this->value = explode(',',$this->value);
    }

    /**
     * table表格内容
     * @param $key
     * @param $value
     */
    public function checkTableValue($key, $value)
    {
        $value = parent::checkTableValue($key, $value);
        return BrandHelper::arrayFindInArray(BrandHelper::string2array($value),$this->getTableParam('options'));
    }

    /**
     * 插入验证
     * @param $key
     * @param $value
     */
    public function checkUpdateData($key, $value)
    {
        return BrandHelper::array2String($value);
    }

    /**
     * 创建Curd表单
     * @param $field
     * @param $props
     */
    public function DbToCurd($field,$props = [])
    {
        $column = \brand\Brand::Builder()->form_props($props);
        $comment = Database::curdComment($field['comment']);

        if($comment['params']){

            $column->form_method('checkbox')
                ->form_options($comment['params'])
                ->table_action_type('checkbox')
                ->table_options($comment['options'] ?? $comment['params'] );
        }

        return $column;
    }

    public function buildConfigColumn($tableConfigValue, $curd_method = 'curd_method')
    {
        return $this->buildSelectConfig($tableConfigValue,'Checkbox',$curd_method);
    }

    public function methodFormConf($form,$table,$params)
    {
        $param = $params['param'] ?? [];
        $param = BrandHelper::optionsStrToInt($param);
        $form['options'] = $param;
        $table['action_type'] = 'checkbox';
        $table['options'] = BrandHelper::encodeOptions($param);

        return [$form,$table];
    }

}